import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Center from '../views/Center'
import SetUp from '../views/SetUp.vue'
import Sort from '../views/Sort.vue'
import Car from '../views/Car.vue'
import store from '../store/index'
import Address from '../views/Address'
import Safe from '../views/Safe'
import OrderTopic from '../views/OrderTopic.vue'
import OutGood from '../views/OutGood.vue'
import Sign from '../views/Sign.vue'
import Welfare from '../views/Welfare.vue'
import Insurance from '../views/Insurance.vue'
import Collection from '../views/Collection.vue'
import Form from '../views/Form.vue'
import List from '../views/List'
import Order from '../components/Order'
import Detail from '../components/Detail.vue'
import Brand from'../views/Brand.vue'

Vue.use(VueRouter)

let router = new VueRouter({
  routes: [
    {
      path: '/', 
      component: () => import('../views/Main.vue'),
      children: [
        {
          path: '/', //首页
          component: Home
        },
        {

          path: 'center', //个人中心
          component: Center,
        },
        {
          path: 'brand',//分类中的品牌中心
          component: Brand,
        },

        
      
        {
          path: 'sort', //分类
          component: Sort
        },
        {
          path: 'list', //商品列表
          component: List
        },
        {
          path: 'car',//购物车
          component: Car
        },
        {
          path: '/order',
          component: Order
        },
      ]
    },


    {
      path: '/login', //登录
      component: () => import('../views/Login.vue')
    },
    {
      path: '/register', //注册
      component: () => import('../views/Register.vue')
    },
    {
      path: '/detail/:gid',//详情页
      component: Detail
    },


    {
      path: '/setup', //设置
      component: SetUp
    },
    {
      path: '/address',//地址管理
      component: Address
    },
    {
      path: '/safe', //安全页
      component: Safe
    },

    {
      path: '/infoadd/:index', //地址增加&修改
      component:()=>import('../components/Infoadd.vue')
    },
    {
      path: '/orderTopic/0',
      component: OutGood, //海外
    },
    {
      path: '/orderTopic/1',
      component: Sign,  //抽奖
    },
    {
      path: '/orderTopic/2',
      component: Welfare,  //公益
    },
    {
      path: '/orderTopic/3',
      component: Insurance,  //保险
    },
    {
      path: '/orderTopic/4',
      component: OrderTopic,  //周期配送
    },
    {
      path: '/collection',
      component: Collection,//收藏
    },
    {
      path: '/form',
      component: Form,//结算页
    }
  ]
})

//路由守卫
router.beforeEach((to, from, next) => {
  // console.log(to,from);
  if (to.path.indexOf('/car')!= -1 ||to.path.indexOf('/center')!= -1){
    if (store.state.user.userInfo) {
      next()
    } else {
      next('/login')
    }
    return
  }

  next()
})

export default router
